package com.card.hwatt.controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.card.hwatt.assembler.DepartmentAssembler;
import com.card.hwatt.service.DepartmentService;
import com.card.hwatt.dto.*;

/**
 * 部门
 * @author Administrator
 *
 */
@Controller
@Scope("prototype")
@RequestMapping("/department")
public class DepartmentController extends BaseController{

	@Autowired
	@Qualifier("departmentService")
	private DepartmentService departmentService;

	@RequestMapping("/loadAll")
	public @ResponseBody
	List<TreeEntity> loadAll(Long id) {
		if (id == null) {

			List<TreeEntity> list = DepartmentAssembler
					.convertTree(departmentService.loadAll(new PageParam(0, 0,
							" orderBy asc ")).rows);
			return list;
		} else {
			List<Object> params = new ArrayList<Object>();
			params.add("%@" + id + "@%");

			List<TreeEntity> list = DepartmentAssembler
					.convertTree(departmentService.loadAll(new PageParam(0, 0,
							" path like ? ", params, " orderBy asc ")).rows);
			return list;
		}
	}

	@RequestMapping("/delete")
	public @ResponseBody
	ReturnValue delete(Long id) {
		departmentService.delete(id);
		ReturnValue result = new ReturnValue();
		result.message = "ok";
		result.isSucess = true;
		return result;
	}
}
